<?
	function showserver() {
		exec('grep -e "local_enable" -e "guest_enable" -e "pasv_address" -e "pasv_min_port" -e "pasv_max_port" /etc/vsftpd.conf',$out,$ret);
		foreach ($out as $o) {
			#if(trim($o) == 'local_enable=YES')
			#	$local = 'checked';
			if(trim($o) == 'guest_enable=YES')
				$guest = 'checked';
			if(substr(trim($o),0,13) == 'pasv_address=')
				$pasv_address = substr($o,13);
			if(substr(trim($o),0,14) == 'pasv_min_port=')
				$pasv_min_port = substr($o,14);
			if(substr(trim($o),0,14) == 'pasv_max_port=')
				$pasv_max_port = substr($o,14);
		}
		unset($out);
		exec('grep -e "pam_ldap.so" /etc/pam.d/vsftpd',$out,$ret);
		foreach ($out as $o) {
			if(substr(trim($o),0,1) != '#')
				$local = 'checked';
		}
		echo '
			<table>
			<tr><td class="tbhead" colspan="2">Användare som får logga in</td></tr>
			<tr '.help('local').'><td>Lokala användare</td><td><input name="local" type="checkbox" '.$local.'></td></tr>
			<tr '.help('virtual').'><td>Virtuella användare</td><td><input name="virtual" type="checkbox" '.$guest.'></td></tr>
			</table>
			<br>
		';
		echo '
			<table>
			<tr><td class="tbhead" colspan="2">Inställningar för passiv server</td></tr>
			<tr '.help('passiveaddress').'><td>Yttre adress:</td><td><input name="pasv_address" value="'.$pasv_address.'"></td></tr>
			<tr '.help('passiveports').'><td>Portintervall data:</td><td><input name="pasv_min_port" size="5" value="'.$pasv_min_port.'"> - <input name="pasv_max_port" size="5" value="'.$pasv_max_port.'"></td></tr>
			</table>
			<br>
		';
		echo '<input type="submit" value="Spara" name="b_OK" class="button">';
	}


	function showusers() {
		$sharedir = '/var/shares/';
		$user_conf = '/etc/vsftpd_user_conf';
		$scan = scandir($sharedir);
		foreach ($scan as $s) {
			if (is_dir($sharedir.$s) && ($s != '.' && $s != '..')) {
				$options .= '<option>'.$s.'</option>';
			}
		}

		$vuserfile = '/etc/vsftpd.passwd';
		$lines = file($vuserfile);
		foreach ($lines as $l) {
			$uarr = explode(':', $l);
			$vusers[] = $uarr[0];
		}

		echo '
			<table style="float:right; font-size:0.8em; border:1px solid #777">
			<tr><td style="background:#cce; border-bottom:1px solid #777;">Rättigheter</td></tr>
			<tr><td style="background:#7c7; padding:5px;">Enbart läsa</td></tr>';
			#<tr><td style="background:#ee7; padding:5px;">Ladda upp</td></tr>
			echo '<tr><td style="background:#c77; padding:5px;">Ändra och ta bort</td></tr>
			</table>
		';


		if (isset($vusers)) {
			echo '
				<table><tr><td colspan="2">
				<tr><td class="tbhead">Användarnamn</td><td class="tbhead">Rot-katalog</td><td class="tbhead" colspan="2">Rättigheter</td><td class="tbhead">Ta bort</td></tr>
			';
			foreach ($vusers as $vu) {
				$i++;
				unset($vus_we);
				unset($vus_aue);
				unset($vus_awro);
				$vusettings = file($user_conf.'/'.$vu);
				foreach ($vusettings as $vus) {
					$vus = trim($vus);
					if (substr($vus,0,11) == 'local_root=') {
						$vus_lr = substr($vus,11);
						$vus_lr = explode('/',str_replace($sharedir, '', $vus_lr),2);
						$vus_options = str_replace('<option>'.$vus_lr[0], '<option selected>'.$vus_lr[0], $options);
					}
					if ($vus == 'write_enable=YES') {
						$vus_we = 'checked';
					} elseif ($vus == 'anon_upload_enable=YES') {
						$vus_aue = 'checked';
					} elseif ($vus == 'anon_world_readable_only=NO') {
						$vus_awro = 'checked';
					}
				}
				echo '
					<tr style="background:'.($i%2==0?'#eef':'#dde').'"><td>'.$vu.'</td>
					<td><select '.help('share').' name="directory.'.$vu.'">'.$vus_options.'</select><input '.help('subdir').' name="subdirectory.'.$vu.'" value="'.$vus_lr[1].'"></input></td>
					<td '.help('rights').' style="background:#7c7; border-left:1px solid #777;"><input type="radio" name="rights.'.$vu.'" value="awro" '.$vus_awro.'></td>';
					#<td style="background:#ee7;"><input type="radio" name="rights.'.$vu.'" value="aue" '.$vus_aue.'></td>
					echo '<td '.help('rights').'  style="background:#c77; border-right:1px solid #777;"><input type="radio" name="rights.'.$vu.'" value="we" '.$vus_we.'></td>
					<td '.help('delete').' style="text-align:center"><a href="main.php?s=ftp&t=users&b_delete='.$vu.'&do=1"><img src="images/delete.png"></a></td></tr>
				';
			}
			echo '</table>';
			echo '<br><input type="submit" value="Spara" name="b_OK" class="button"><br><br>';
		}


		echo "<table  cellspacing=\"0\" style=\"border: 1px solid #bbb; width: 25em; padding: 0.5em;\" ".help('add').">";
		echo "<tr><td colspan=2><b>Skapa ny</b></td></tr>";
		echo "<tr><td>användarnamn:</td><td style=\"text-align: left;\"><input class=\"input-box\" type=\"text\" name=\"name\" style=\"width: 8em\"></td></tr>\n";
		echo "<tr><td>lösenord:</td><td style=\"text-align: left;\"><input class=\"input-box\" type=\"password\" name=\"pass1\" style=\"width: 8em\"></td></tr>\n";
		echo "<tr><td>lösenord igen:</td><td style=\"text-align: left;\"><input class=\"input-box\" type=\"password\" name=\"pass2\" style=\"width: 8em\"></td></tr>\n";
		echo '<tr><td>Rot-katalog:</td><td><select '.help('share').' name="directory">'.$options.'</select><input '.help('subdir').' name="subdirectory" value="'.$vus_lr[1].'"></input></td></tr>';
		echo "<tr><td></td><td style=\"text-align: left; vertical-align: bottom;\"><input type=\"submit\" name=\"b_add\" value=\"skapa\" class=\"button\" /></td></tr>";
		echo "</table><br>";


	}


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

	html_start( array(
		array('server', 'Ftp-server'), 
		array('users', 'Virtuella användare'),
	));

	switch( $_GET['t'] ) {
	default:
	case 'server':
		showserver();
		html_end();
		break;
	case 'users':
		showusers();
		html_end();
		break;
	}
?>
